package cn.gson.school.controller;

import javax.persistence.Index;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.alibaba.fastjson.JSONArray;

import cn.gson.school.dao.ImagesDao;
import cn.gson.school.dao.jiaoxue.AnswerDao;
import cn.gson.school.dao.jiaoxue.ClassDao;
import cn.gson.school.dao.jiaoxue.GrandeDao;
import cn.gson.school.dao.jiaoxue.PhaseDao;

import static org.apache.commons.lang.StringUtils.isEmpty;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;

import cn.gson.school.conmmon.Menu;
import cn.gson.school.controller.xitong.AjaxResult;
import cn.gson.school.dao.jiaoxue.StudentDao;
import cn.gson.school.dao.jiaoxue.TiMuDao;
import cn.gson.school.dao.jiaoxue.ZhangJieDao;
import cn.gson.school.dao.jiaoxue.ZuoYeDao;
import cn.gson.school.dao.jiaoxue.ZuoYeTitleDao;
import cn.gson.school.dao.xitong.DeptSerchDao;
import cn.gson.school.dao.xitong.ResourcesDao;
import cn.gson.school.dao.xitong.RoleSerchDao;
import cn.gson.school.dao.xitong.UserDao;
import cn.gson.school.dao.xitong.UserInfoDao;
import cn.gson.school.dao.zhaosheng.InputStudentDao;
import cn.gson.school.entity.jiaoxue.*;
import cn.gson.school.entity.jiuye.InterviewRecordEntity;
import cn.gson.school.entity.jiuye.OfferEntity;
import cn.gson.school.entity.xitong.*;
import cn.gson.school.entity.zhaosheng.InputStudentEntity;
import cn.gson.school.mappers.jiaoxue.TiMuMappers;
import cn.gson.school.mappers.jiaoxue.ZuoYeMappers;
import cn.gson.school.mappers.jiuye.OfferEntityMapper;
import cn.gson.school.mappers.qiantai.LearnPlanMapper;
import cn.gson.school.mappers.xitong.EmpMapper;
import cn.gson.school.mappers.xitong.ResourceMapper;
import cn.gson.school.mappers.xitong.UserMapper;
import cn.gson.school.service.ImagesService;

@SuppressWarnings("unused")
@Controller
@RequestMapping("/")
public class AppController {
	Logger log = LoggerFactory.getLogger(getClass());
	@Autowired
	private UserDao userDao;

	@Autowired
	private StudentDao stuDao;

	@Autowired
	private ClassDao classDao;
	
	@Autowired
	private UserInfoDao empDao;
	
	@Autowired
	private RoleSerchDao roleDao;

	@Autowired
	private DeptSerchDao deptDao;

	@Autowired
	private ImagesDao imgDao;

	@Autowired
	private UserMapper userMapper;

	@Autowired
	private EmpMapper empMapper;

	@Autowired
	private ImagesService imgService;

	@Autowired
	private LearnPlanMapper learnMapper;

	@Autowired
	private UserInfoDao userinfoDao;
	
	@Autowired
	private TiMuMappers timuMappers;
	
	@Autowired
	private TiMuDao timuDao;
	
	@Autowired
	private ZuoYeMappers zuoyeMappers;
	
	@Autowired
	private ZuoYeTitleDao zuoYeTitleDao;
	
	@Autowired
	private AnswerDao answerDao;
	
	@Autowired
	private GrandeDao grandeDao;
	
	@Autowired
	private PhaseDao phaseDao;

	@Autowired
	private ZhangJieDao zhangJieDao;
	
	@Autowired
	private ZuoYeDao zuoyeDao;
	
	@Autowired
	private InputStudentDao inputStudentDao;
	
	@Autowired
	private OfferEntityMapper offerEntityMapper;


	@Autowired
	private ResourcesDao resDao;
	
	@Autowired
	private ResourceMapper resMapper;
	
	@Value("${system.super.user.userId}")
	private Long superId;
	
	@RequestMapping("zuoye/")
	public void jiaoZuoye() {
	}

	/**
	 * 默认地址
	 * 
	 * @return
	 */
	@RequestMapping
	public String index() {
		return "login";
	}

	@GetMapping("/login")
	public String login(@SessionAttribute(required = false, value = "user") UserEntity user) {
		if (user != null) {
			// 已登录用户，返回login请求，则重定向回首页
			String shenfen = userMapper.shenfen(user.getUserId());
			if (("学生").equals(shenfen)) {
				return "redirct:/qiantai";
			} else {
				return "redirect:/zhujiemian";
			}

		}
		return "login";
	}

	/*	@PostMapping("/login")
	public String doLogin(@RequestParam String userAccount, @RequestParam String userPass,
			RedirectAttributes attributes, HttpSession session) {

		// 获取对象
		UserEntity user = userDao.findByUserAccount(userAccount);
		System.out.println("user"+user);
		// 判断账号密码是否为空，为空则返回到登录界面否则判断用户是否为空或是否已删除，是则返回主界面

		if ("".equals(userAccount) || "".equals(userPass)) {
			attributes.addFlashAttribute("error", "请输入账号和密码！");
			attributes.addFlashAttribute("userAccount", userAccount);
			return "redirect:/login";
		} else if (user == null || user.getDelete()  == 0) {
			attributes.addFlashAttribute("error", "账号或密码错误！");
			attributes.addFlashAttribute("userAccount", userAccount);
			System.out.println(userAccount);
			return "redirect:/login";
		} else {
			// 判断密码是否正确，不正确或为空则返回到登录界面

			if (user != null && user.getUserPass().equals(DigestUtils.md5Hex(userPass))) {
				if(user.getEnable() == false) {
					attributes.addFlashAttribute("error", "账号不可用！");
					attributes.addFlashAttribute("userAccount", userAccount);
					return "redirect:/login";
				}else {

					String shenfen = userMapper.shenfen((Long) user.getUserId());
					// 判断身份是否为学生，是学生则跳转到前台主界面，否则则跳转到后台主界面，并将用户存入session作用域

					if (("学生").equals(shenfen)) {
						// 用户涉及的权限

						Set<ResourcesEntity> resources;
						// 判断是否是超级管理员

						if (Objects.equals(superId, user.getUserId())) {
							resources = resDao.findAllByEnableOrderByWeightDesc(true);
						} else {
							// 获取用户菜单

							List<RoleEntity> roles = user.getRoles();
							resources = new HashSet<>();
							roles.forEach(role -> resources.addAll(role.getResources()));
						}

						// 存储菜单

						TreeSet<ResourcesEntity> menus = new TreeSet<>((o1, o2) -> {
							if (Objects.equals(o1.getWeight(), o2.getWeight())) {
								return -1;
							}
							return o1.getWeight() - o2.getWeight();
						});

						// 存储权限key

						Set<String> keys = new HashSet<>();
						// 所有有权限访问的请求

						Set<String> urls = new HashSet<>();

						resources.forEach(resource -> {
							if (resource.getEnable()) {
								if (resource.getType().equals(ResourcesEntity.Type.MENU)) {
									// 是菜单

									menus.add(resource);
									urls.add(resource.getMenuPath());
								}
								// 获取用户拥有的权限

								keys.add(resource.getPermissionKey());

								urls.addAll(Arrays.asList(resource.getResource().split(",")));
							}
						});

						// 树形数据转换

						List<Menu> menuList = new ArrayList<>();
						menus.forEach(resource -> {
							Menu m = new Menu();
							m.setId(resource.getResourceId());
							m.setText(resource.getResourceName());
							m.setHref(resource.getMenuPath());
							m.setParentId(resource.getParent() == null ? null : resource.getParent().getResourceId());
							menuList.add(m);
						});

						session.setAttribute("user", user);
						session.setAttribute("menus", menuList);
						session.setAttribute("keys", keys);
						session.setAttribute("urls", urls);

						List<JxLogonLogTab> denglurzlist = jxLianxiMapper.selectdenglu(user.getZdStuMess().getId());
						boolean pd = false;
						for (int i = 0; i < denglurzlist.size(); i++) {
							if (denglurzlist.get(i).getLogonLogTime().toLocaleString()
									.substring(0, denglurzlist.get(i).getLogonLogTime().toLocaleString().indexOf(" "))
									.equals(new Date().toLocaleString().substring(0,
											new Date().toLocaleString().indexOf(" ")))) {
								JxLogonLogTab ll = jxLogonLogDao.findOne(denglurzlist.get(i).getLogonLogId());
								ll.setLogonLogTime(new Date());
								ll.setLogonLogCount(ll.getLogonLogCount() + 1);
								jxLogonLogDao.save(ll);
								pd = true;
							}
						}

						if (pd == false) {
							JxLogonLogTab denglurz = new JxLogonLogTab();
							denglurz.setIsDelete(false);
							denglurz.setLogonLogStu(user.getZdStuMess());
							denglurz.setLogonLogTime(new Date());
							denglurz.setLogonLogCount(1);
							jxLogonLogDao.save(denglurz);
						}

						return "redirect:/zhujiemian";
					} else {
						// 后台用户

						// 用户涉及的权限

						Set<ResourcesEntity> resources;
						// 判断是否是超级管理员

						if (Objects.equals(superId, user.getUserId())) {
							resources = resDao.findAllByEnableOrderByWeightDesc(true);
						} else {
							// 获取用户菜单

							List<RoleEntity> roles = user.getRoles();
							resources = new HashSet<>();
							roles.forEach(role -> resources.addAll(role.getResources()));
						}

						// 存储菜单

						TreeSet<ResourcesEntity> menus = new TreeSet<>((o1, o2) -> {
							if (Objects.equals(o1.getWeight(), o2.getWeight())) {
								return -1;
							}
							return o1.getWeight() - o2.getWeight();
						});

						// 存储权限key

						Set<String> keys = new HashSet<>();
						// 所有有权限访问的请求

						Set<String> urls = new HashSet<>();

						resources.forEach(resource -> {
							if (resource.getEnable()) {
								if (resource.getType().equals(ResourcesEntity.Type.MENU)) {
									// 是菜单

									menus.add(resource);
									urls.add(resource.getMenuPath());
								}
								// 获取用户拥有的权限

								keys.add(resource.getPermissionKey());

								urls.addAll(Arrays.asList(resource.getResource().split(",")));
							}
						});

						// 树形数据转换

						List<Menu> menuList = new ArrayList<>();
						menus.forEach(resource -> {
							Menu m = new Menu();
							m.setId(resource.getResourceId());
							m.setText(resource.getResource());
							m.setHref(resource.getMenuPath());
							m.setParentId(resource.getParent() == null ? null : resource.getParent().getResourceId());
							menuList.add(m);
						});

						session.setAttribute("user", user);
						session.setAttribute("menus", menuList);
						session.setAttribute("keys", keys);
						session.setAttribute("urls", urls);

						session.setAttribute("userName", user.getUserAccount());
						
						return "redirect:/zhujiemian";
					}
				
				}
			} else {
				attributes.addFlashAttribute("error", "账号或密码错误！");
				attributes.addFlashAttribute("userAccount", userAccount);
				return "redirect:/login";
			}
		}
	}
	*/
	@PostMapping("/login")
	public String doLogin(@RequestParam String userAccount, @RequestParam String userPass,
			RedirectAttributes attributes, HttpSession session) {
		// 获取对象
		UserEntity user = userDao.findByUserAccount(userAccount);
		System.out.println("user:" + user);
		// 判断账号密码是否为空，为空则返回到登录界面否则判断用户是否为空或是否已删除，是则返回主界面
			if("".equals(userAccount)||"".equals(userPass)) {
				attributes.addFlashAttribute("error", "请输入账号和密码！");
				attributes.addFlashAttribute("userAccount", userAccount);
				return "redirect:/login";
			}else if(user==null||user.getDelete()==0){
				attributes.addFlashAttribute("error", "账号或密码错误！");
				attributes.addFlashAttribute("userAccount", userAccount);
				return "redirect:/login";
			}else {
				// 判断密码是否正确，不正确或为空则返回到登录界面
				if(user!=null&&user.getUserPass().equals(DigestUtils.md5Hex(userPass))) {
					if(user.getEnable()==false) {
						attributes.addFlashAttribute("error", "账号不可用！");
						attributes.addFlashAttribute("userAccount", userAccount);
						return "redirect:/login";
					}else {
						String shenfen = userMapper.shenfen(user.getUserId());
						// 判断身份是否为学生，是学生则跳转到前台主界面，否则则跳转到后台主界面，并将用户存入session作用域
						if(("学生").equals(shenfen)) {
							// 用户涉及的权限
							StudentEntity studentEntity = stuDao.findByUserId(user);
							Long classid = studentEntity.getClassId();
							ClassEntity classEntity = classDao.findOne(classid);
							List<Map<String, Object>> listidentity = learnMapper.listidentity(studentEntity.getStuId());
							if (classEntity != null) {
								StudentEntity monitor = stuDao.findOne(classEntity.getClassBuZhang());
								System.out.println("班长:" + monitor.getStudentName());
								System.out.println(studentEntity);
								session.setAttribute("userinfo", studentEntity);
								session.setAttribute("classEntity", classEntity);
								session.setAttribute("monitor", monitor);
								session.setAttribute("user", user);
								session.setAttribute("identity", listidentity.get(0));
							}
							// 用户涉及的权限
							Set<ResourcesEntity> resources;
							// 判断是否是超级管理员
							if(Objects.equals(superId, user.getUserId())) {
								resources=resDao.findAllByEnableOrderByWeightDesc(true);
							}else {
								// 获取用户菜单
								List<RoleEntity> roles=user.getRoles();
								resources=new HashSet<>();
								roles.forEach(role -> resources.addAll(role.getResources()));
							}
							// 存储菜单
							TreeSet<ResourcesEntity> menus = new TreeSet<>((o1, o2) -> {
								if (Objects.equals(o1.getWeight(), o2.getWeight())) {
									return -1;
								}
								return o1.getWeight() - o2.getWeight();
							});
							// 存储权限key
							Set<String> keys = new HashSet<>();
							// 所有有权限访问的请求
							Set<String> urls = new HashSet<>();
							resources.forEach(resource -> {
								if (resource.getEnable()) {
									if (resource.getType().equals(ResourcesEntity.Type.MENU)) {
										// 是菜单

										menus.add(resource);
										urls.add(resource.getMenuPath());
									}
									// 获取用户拥有的权限

									keys.add(resource.getPermissionKey());

									urls.addAll(Arrays.asList(resource.getResource().split(",")));
								}
							});
							// 树形数据转换

							List<Menu> menuList = new ArrayList<>();
							menus.forEach(resource -> {
								Menu m = new Menu();
								m.setId(resource.getId());
								m.setText(resource.getResourceName());
								m.setHref(resource.getMenuPath());
								m.setParentId(resource.getParent() == null ? null : resource.getParent().getResourceId());
								menuList.add(m);
							});
							session.setAttribute("user", user);
							session.setAttribute("menus", menuList);
							session.setAttribute("keys", keys);
							session.setAttribute("urls", urls);
							return "redirect:/qiantai";
						}else {
							// 后台用户
							// 用户涉及的权限
							Set<ResourcesEntity> resources;
							// 判断是否是超级管理员
							if(Objects.equals(superId, user.getUserId())) {
								resources=resDao.findAllByEnableOrderByWeightDesc(true);
							}else {
								// 获取用户菜单
								List<RoleEntity> roles=user.getRoles();
								resources=new HashSet<>();
								roles.forEach(role -> resources.addAll(role.getResources()));
							}
							// 存储菜单
							TreeSet<ResourcesEntity> menus = new TreeSet<>((o1, o2) -> {
								if (Objects.equals(o1.getWeight(), o2.getWeight())) {
									return -1;
								}
								return o1.getWeight() - o2.getWeight();
							});
							// 存储权限key
							Set<String> keys = new HashSet<>();
							// 所有有权限访问的请求
							Set<String> urls = new HashSet<>();
							resources.forEach(resource -> {
								if (resource.getEnable()) {
									if (resource.getType().equals(ResourcesEntity.Type.MENU)) {
										// 是菜单

										menus.add(resource);
										urls.add(resource.getMenuPath());
									}
									// 获取用户拥有的权限

									keys.add(resource.getPermissionKey());

									urls.addAll(Arrays.asList(resource.getResource().split(",")));
								}
							});
							// 树形数据转换

							List<Menu> menuList = new ArrayList<>();
							menus.forEach(resource -> {
								Menu m = new Menu();
								m.setId(resource.getId());
								m.setText(resource.getResourceName());
								m.setHref(resource.getMenuPath());
								m.setParentId(resource.getParent() == null ? null : resource.getParent().getResourceId());
								menuList.add(m);
							});
							
							UserInfoEntity userInfoEntity = userinfoDao.findByUserId(user);
							ImageEntity headimg = imgDao.findOne(user.getImagesId());
							
							session.setAttribute("user", user);
							session.setAttribute("menus", menuList);
							session.setAttribute("keys", keys);
							session.setAttribute("urls", urls);
							
							session.setAttribute("userinfo", userInfoEntity);
							session.setAttribute("headimg", headimg);// 保存头像
							session.setAttribute("userinfoid", userInfoEntity.getUserInfoId());
							session.setAttribute("userAccount", user.getUserAccount());
							return "redirect:/zhujiemian";
						}
					}
				}else {
					attributes.addFlashAttribute("error", "账号或密码错误！");
					attributes.addFlashAttribute("userAccount", userAccount);
					return "redirect:/login";
				}
			}
	}

	/**
	 * 注销
	 */
	@GetMapping("/logout")
	public String logout(HttpSession session, HttpServletRequest request, HttpServletResponse response) {
		session.invalidate();
		// 清除cookie
		Cookie[] cookies = request.getCookies();
		for (int i = 0; i < cookies.length; i++) {
			Cookie cookie = new Cookie(cookies[i].getName(), null);
			cookie.setMaxAge(0);
			response.addCookie(cookie);
		}
		// 注销之后，重新跳回登录请求
		return "redirect:/login";

	}

	@RequestMapping("zhujiemian")
	public String zhujiemian(Model model) {
		//录入学生的数量
				List<InputStudentEntity> f=inputStudentDao.chaXunAllInputStudent();
				System.out.println("录入学生的长度是："+f.size());
				//在读中学生的数量
				List<StudentEntity> count1=stuDao.findByStudentState(1+"");
				System.out.println("在读中学生的长度是："+count1.size());
				//已退学学生的数量
				List<StudentEntity> count2=stuDao.findByStudentState(2+"");
				System.out.println("已退学学生的长度是："+count2.size());
				//就业中学生的数量
				List<StudentEntity> count3=stuDao.findByStudentState(3+"");
				System.out.println("就业中学生的长度是："+count3.size());
				//已就业学生的数量
				List<StudentEntity> count4=stuDao.findByStudentState(4+"");
				System.out.println("已就业学生的长度是："+count4.size());
				
				model.addAttribute("inputStudentCount",JSONArray.toJSONString(f.size()));
				model.addAttribute("count1",JSONArray.toJSONString(count1.size()));
				model.addAttribute("count2",JSONArray.toJSONString(count2.size()));
				model.addAttribute("count3",JSONArray.toJSONString(count3.size()));
				model.addAttribute("count4",JSONArray.toJSONString(count4.size()));
				return "zhujiemian";
	}
	
	@RequestMapping("reject")
	public String reject() {
		return "xitongt/reject";
	}
	
	@RequestMapping("/menus")
	@ResponseBody
	public List<Menu> menus(@SessionAttribute("menus") List<Menu> menuList) {
		return menuList;
	}

	@RequestMapping("qiantai")
	public String qiantai(@SessionAttribute(required = false, value = "userinfo") StudentEntity studentEntity,Model model) {
		try {
		//获取未提交作业
		List<Map<String, Object>> maps= zuoyeMappers.findByStudent(studentEntity.getStuId());
		//获取未提交考试
		List<Map<String, Object>> maps2= zuoyeMappers.findByStudent2(studentEntity.getStuId());
		//获取已提交作业
		List<Map<String, Object>> maps3= zuoyeMappers.findByStudent3(studentEntity.getStuId());
		//获取已提交考试
		List<Map<String, Object>> maps4= zuoyeMappers.findByStudent4(studentEntity.getStuId());
		int i=1;
		List<Map<String, Object>> jihe;
		List<InterviewRecordEntity> qiantaichaxunzuixinjiuye;
		System.out.println("asdasd");

			//查询排名
			List<Map<String, Object>> qianTaiChaXuPaiMing = offerEntityMapper.qiantaichaxunpaiming();
			System.out.println("asdasda");
			jihe = null;
			qiantaichaxunzuixinjiuye = null;

				jihe = new ArrayList<Map<String, Object>>();
				for (Map<String, Object> map : qianTaiChaXuPaiMing) {
					Map<String, Object> maps5 = map;
					if (i == 1) {
						maps5.put("paiming", "冠军");
					} else if (i == 2) {
						maps5.put("paiming", "亚军");
					} else {
						maps5.put("paiming", "季军");
					}
					jihe.add(maps5);
					i++;
				}
				qiantaichaxunzuixinjiuye = offerEntityMapper.qiantaichaxunzuixinjiuye();
			model.addAttribute("jihe", jihe);
			model.addAttribute("zuixinjieye", qiantaichaxunzuixinjiuye);
			System.out.println("asdasd");
		/*		System.out.println("KK:"+studentEntity.getStuId());
		System.out.println("maps:"+maps); 
		System.out.println("maps2:"+maps2);
		System.out.println("maps3:"+maps3);
		System.out.println("maps4:"+maps4);
		*/
		model.addAttribute("maps", maps);
		model.addAttribute("maps2", maps2);
		model.addAttribute("maps3", maps3);
		model.addAttribute("maps4", maps4);
		model.addAttribute("studentinfoId", studentEntity.getStuId());
		
		} catch (Exception e) {
			System.out.println("asdasdc");
		}
		return "qiantai";
	} 

	@RequestMapping("/xt-userinfo")
	public void userinfo() {

	}

	/**
	 * 修改密码
	 */
	@RequestMapping("/change/password")
	public String changePassword() {
		return "xt-editpass";
	}

	/**
	 * 修改密码
	 */
	@RequestMapping(value = "/change/password", method = RequestMethod.POST)
	@Transactional
	@ResponseBody
	public AjaxResult doChangePassword(@SessionAttribute(name = "user") UserEntity user, String oldPass,
			String newPass1, String newPass2) {
		System.out.println("duduuddu" + user);
		if (isEmpty(oldPass) || isEmpty(newPass1) || isEmpty(newPass2)) {
			return new AjaxResult(false).setMessage("参数错误！");
		}

		if (!user.getUserPass().equals(DigestUtils.md5Hex(oldPass))) {
			return new AjaxResult(false).setMessage("原密码错误！");
		}

		if (!DigestUtils.md5Hex(newPass1).equals(DigestUtils.md5Hex(newPass2))) {
			return new AjaxResult(false).setMessage("新密码，两次不匹配！");
		}
		System.err.println("sfasf" + user);
		UserEntity u = userDao.findOne(user.getUserId());
		System.out.println("111111111111111111111" + u);
		u.setUserPass(DigestUtils.md5Hex(newPass1));
		System.out.println("ffsgsegdf" + u);
		userDao.save(u);

		return new AjaxResult();
	}

	/**
	 * 修改当前用户的资料
	 */
	@GetMapping("/change/userinfo")
	public String info(Model model) {
		return "xt-userinfo";
	}

	/**
	 * 修改当前用户的资料
	 */
	@PostMapping("change/info")
	@ResponseBody
	public AjaxResult doChangeInfo(@RequestParam("imagePath") MultipartFile file, HttpSession session,
			HttpServletRequest request, Model model) {
		System.out.println("你进来了码？");
		/*
		 * if (isEmpty(emp.getUserName()) || isEmpty(emp.getUserSex()) ||
		 * emp.getUserTel() == null) { return new AjaxResult(false).setMessage("参数错误！");
		 * }
		 */
		// 获取隐藏域的id
		String userid = request.getParameter("userId");
		String userAccount = request.getParameter("userAccount");
		String userName = request.getParameter("userName");
		String registTime = request.getParameter("registTime");
		String userSex = request.getParameter("userSex");
		String userTel = request.getParameter("userTel");
		String deptName = request.getParameter("deptName");

		System.out.println("userT===" + userTel);
		System.out.println("userT===" + userTel);
		UserEntity user1 = userDao.findOne(Long.valueOf(userid));
		UserInfoEntity emp2 = empDao.findByUserId(user1);
		System.out.println("user+==" + user1);
		System.out.println("emp2+==" + emp2);

		ImageEntity img = null;
		img = imgDao.findOne(user1.getImagesId());
		System.out.println("图片：" + img);
		String oldTouxiang = img.getImagePath();
		System.out.println("头像"+oldTouxiang);
		
		System.out.println("file.getSize()" + file.getSize());
		if (file.getSize() != 0) {
			try {
				img = imgService.upload(user1, "xt", file,"");
				System.out.println("图片：" + img);
				
				user1.setImagesId(img.getImageId());
				
				imgService.drop(oldTouxiang);
			} catch (Exception s) {
			}
		}
		emp2.setUserSex(userSex);
		emp2.setUserName(userName);
		emp2.setUserTel(userTel);
		session.setAttribute("userinfo", emp2);
		session.setAttribute("user", user1);
		session.setAttribute("headimg", img);
		empDao.save(emp2);
		return new AjaxResult();
	}

	/**
	 * 前台修改密码
	 * 
	 * @return
	 */
	@RequestMapping("/qt-user-updatepass")
	public String updatepass() {

		return "qt-user-updatepass";

	}

	@PostMapping("/qt-user-updatepass")
	public String doPassword(@SessionAttribute(name = "user") UserEntity user, String oldsPass, String newsPass1,
			String newsPass2, RedirectAttributes redirectAttributes) {
		System.out.println("duduuddu" + user);
		if (isEmpty(oldsPass) || isEmpty(newsPass1) || isEmpty(newsPass2)) {
			redirectAttributes.addFlashAttribute("error", "密码输入错误！");

		} else if (!user.getUserPass().equals(DigestUtils.md5Hex(oldsPass))) {
			redirectAttributes.addFlashAttribute("error", "原密码输入错误！");
			return "redirect:/qt-user-updatepass";

		} else if (!DigestUtils.md5Hex(newsPass1).equals(DigestUtils.md5Hex(newsPass2))) {
			redirectAttributes.addFlashAttribute("error", "两次密码输入不一致！");
			return "redirect:/qt-user-updatepass";
		} else {
			System.err.println("sfasf" + user);
			UserEntity u = userDao.findOne(user.getUserId());
			System.out.println("111111111111111111111" + u);
			u.setUserPass(DigestUtils.md5Hex(newsPass1));
			System.out.println("ffsgsegdf" + u);
			userDao.save(u);
		}
		return "redirect:/logout";
	}

	@RequestMapping("/qt-my-honor")
	public void myHonor() {

	}

	@RequestMapping("/qt-all-honor")
	public void addHonor() {

	}
	@GetMapping("/qt-unfinished")
	public String addfind(HttpServletRequest request,Model model) {
		String studentId=request.getParameter("studentId");
		String zuoyeId=request.getParameter("zuoyeId");
		ZuoYeEntity zuoYe=zuoyeDao.findOne(Long.valueOf(zuoyeId));
		StudentEntity studentEntity=stuDao.findOne(Long.valueOf(studentId));
		ClassEntity classEntity=classDao.findOne(studentEntity.getClassId());
		// 根据作业班级里的作业编号，去查询作业题目中的题目，对应的题目
					List<ZuoYeTitleEntity> zuoYeTitles = zuoYeTitleDao.findByZuoyeId(Long.valueOf(zuoyeId));
					System.out.println("获取某个作业下的所有的题目"+zuoYeTitles);
					List<Object> list1 = new ArrayList<Object>();
					List<Object> list2 = new ArrayList<Object>();
					List<Object> list3 = new ArrayList<Object>();
					// 将该作业下的作业循环读出来，存进新的集合中
					for (ZuoYeTitleEntity zuoYeTitleEntity : zuoYeTitles) {
						// 根据作业题目的对象中的题目编号来获取题目对象
						TitleEntity titleEntity = timuDao.findOne(zuoYeTitleEntity.getTitleId());
						// 在根据题目题目对象中的题目类型进行分类，分别存进不同的题目集合中
						if ("选择题".equals(titleEntity.getTitleType())) {
							AnswerEntity answerEntity=answerDao.findByStudentIdAndZuoYeIdAndTitleId(Long.valueOf(studentId), Long.valueOf(zuoyeId), titleEntity.getTitleId());
							if (answerEntity!=null) {
								titleEntity.setTitleType(answerEntity.getAnswer());
							}else {
								titleEntity.setTitleType("无");
							}
							list1.add(titleEntity);
							
						} else if ("判断题".equals(titleEntity.getTitleType())) {
							AnswerEntity answerEntity=answerDao.findByStudentIdAndZuoYeIdAndTitleId(Long.valueOf(studentId), Long.valueOf(zuoyeId), titleEntity.getTitleId());
							if (answerEntity!=null) {
								titleEntity.setTitleType(answerEntity.getAnswer());
							}else {
								titleEntity.setTitleType("无");
							}
							list2.add(titleEntity);
						} else if ("代码题".equals(titleEntity.getTitleType())) {
							AnswerEntity answerEntity=answerDao.findByStudentIdAndZuoYeIdAndTitleId(Long.valueOf(studentId), Long.valueOf(zuoyeId), titleEntity.getTitleId());
							if (answerEntity!=null) {
								titleEntity.setTitleType(answerEntity.getAnswer());
							}else {
								titleEntity.setTitleType("无");
							}
							list3.add(titleEntity);
						}
					}
					System.out.println("list1"+list1);
					for (Object object : list2) {
						System.out.println("list2"+list2);
					}
					System.out.println("list3"+list3);
					model.addAttribute("title1", list1);
					model.addAttribute("title2", list2);
					model.addAttribute("title3", list3);
					model.addAttribute("studentId", studentId);
					model.addAttribute("zuoYe", zuoYe);
					model.addAttribute("className", classEntity.getClassName());
		return "qt-unfinished";
	}

	@RequestMapping("/qt-journal-recommend")
	public void journalRecommend() {

	}

	@RequestMapping("/qt-learnplan-publish")
	public void learnPlanPublish() {

	}

	@RequestMapping("/qt-learnplan-my")
	public void learnplanMy() {

	}

	@RequestMapping("/qt-learnplan-crew")
	public void learnplanCrew() {

	}

	@RequestMapping("/qt-project-team")
	public void projectTeam() {

	}

	@RequestMapping("/qt-project-data")
	public void projectData() {

	}

	@RequestMapping("/qt-project-win")
	public void projectWin() {

	}

	@RequestMapping("/qt-jy-cxjingyan")
	public void cxjingyan() {

	}

	@RequestMapping("/qt-jy-jianli")
	public void jianli() {

	}

	@RequestMapping("/qt-jy-mianshi")
	public void mianshi() {

	}

	@RequestMapping("/qt-jy-cxjilu")
	public void jiLu() {

	}
	

	@RequestMapping("/qt-assessment")
	public String assessment(@SessionAttribute(required = false, value = "userinfo") StudentEntity studentEntity,Model model) {
		//获取已提交的作业和考试
		List<Map<String, Object>> maps= zuoyeMappers.findZuoye(studentEntity.getStuId());
		System.out.println("LLLmap:"+maps);
		model.addAttribute("maps", maps);
		
		//获取练习和考试的次数
		List<GradeEntity> grade=grandeDao.findByStudentinfoId(studentEntity);
		//用来存放成绩为优    90-100为优
		List<GradeEntity> list1=new ArrayList<GradeEntity>();
		//用来存放成绩为良   80-90
		List<GradeEntity> list2=new ArrayList<GradeEntity>();
		//用来存放成绩为中  70-80为中
		List<GradeEntity> list3=new ArrayList<GradeEntity>();
		//用来存放成绩为差  60-70为差
		List<GradeEntity> list4=new ArrayList<GradeEntity>();
		//用来存放成绩为不及格   60分以下
		List<GradeEntity> list5=new ArrayList<GradeEntity>();
		for (GradeEntity gradeEntity : grade) {
			if (gradeEntity.getSumScore()>=90&&gradeEntity.getSumScore()<=100) {
				list1.add(gradeEntity);
			}else if (gradeEntity.getSumScore()>=80&&gradeEntity.getSumScore()<=90) {
				list2.add(gradeEntity);
			}else if (gradeEntity.getSumScore()>=70&&gradeEntity.getSumScore()<=80) {
				list3.add(gradeEntity);
			}else if (gradeEntity.getSumScore()>=60&&gradeEntity.getSumScore()<=70) {
				list4.add(gradeEntity);
			}else {
				list5.add(gradeEntity);
			}
		}
		
		model.addAttribute("count", grade.size());
		model.addAttribute("list1", JSONArray.toJSONString(list1.size()));
		model.addAttribute("list2", JSONArray.toJSONString(list2.size()));
		model.addAttribute("list3", JSONArray.toJSONString(list3.size()));
		model.addAttribute("list4", JSONArray.toJSONString(list4.size()));
		model.addAttribute("list5", JSONArray.toJSONString(list5.size()));
		
		
		//获取班级
		ClassEntity classEntity=classDao.findOne(studentEntity.getClassId());
		model.addAttribute("className", JSONArray.toJSONString(classEntity.getClassName()));
		return "qt-assessment";
	}
	
	//查看已提交练习或者考试
	@RequestMapping("/qt-assignment_details")
	public String assignmentDetails(HttpServletRequest request,Model model) {
		String studentId=request.getParameter("studentId");
		String zuoyeId=request.getParameter("zuoyeId");
		// 根据作业班级里的作业编号，去查询作业题目中的题目，对应的题目
		List<ZuoYeTitleEntity> zuoYeTitles = zuoYeTitleDao.findByZuoyeId(Long.valueOf(zuoyeId));
		System.out.println("获取某个作业下的所有的题目"+zuoYeTitles);
		List<Object> list1 = new ArrayList<Object>();
		List<Object> list2 = new ArrayList<Object>();
		List<Object> list3 = new ArrayList<Object>();
		// 将该作业下的作业循环读出来，存进新的集合中
		for (ZuoYeTitleEntity zuoYeTitleEntity : zuoYeTitles) {
			// 根据作业题目的对象中的题目编号来获取题目对象
			TitleEntity titleEntity = timuDao.findOne(zuoYeTitleEntity.getTitleId());
			// 在根据题目题目对象中的题目类型进行分类，分别存进不同的题目集合中
			if ("选择题".equals(titleEntity.getTitleType())) {
				AnswerEntity answerEntity=answerDao.findByStudentIdAndZuoYeIdAndTitleId(Long.valueOf(studentId), Long.valueOf(zuoyeId), titleEntity.getTitleId());
				if (answerEntity!=null) {
					titleEntity.setTitleType(answerEntity.getAnswer());
				}else {
					titleEntity.setTitleType("无");
				}
				list1.add(titleEntity);
				
			} else if ("判断题".equals(titleEntity.getTitleType())) {
				AnswerEntity answerEntity=answerDao.findByStudentIdAndZuoYeIdAndTitleId(Long.valueOf(studentId), Long.valueOf(zuoyeId), titleEntity.getTitleId());
				if (answerEntity!=null) {
					titleEntity.setTitleType(answerEntity.getAnswer());
				}else {
					titleEntity.setTitleType("无");
				}
				list2.add(titleEntity);
			} else if ("代码题".equals(titleEntity.getTitleType())) {
				AnswerEntity answerEntity=answerDao.findByStudentIdAndZuoYeIdAndTitleId(Long.valueOf(studentId), Long.valueOf(zuoyeId), titleEntity.getTitleId());
				if (answerEntity!=null) {
					titleEntity.setTitleType(answerEntity.getAnswer());
					titleEntity.setDelete(answerEntity.getScore());
				}else {
					titleEntity.setTitleType("无");
				}
				list3.add(titleEntity);
			}
		}
		// 根据学生编号和作业编号来获取分数
		GradeEntity gradeEntity = grandeDao.cxGrande(Long.valueOf(zuoyeId), Long.valueOf(studentId));
		// 获取班级
		ClassEntity classEntity = classDao.findOne(gradeEntity.getStudentinfoId().getClassId());
		String className = classEntity.getClassName();
		model.addAttribute("className", className);
		// 获取作业名称
		ZuoYeEntity zuoye = gradeEntity.getZuoye();
		model.addAttribute("zuoye",zuoye);
	
		// 获取出卷人
		String userinfoName = gradeEntity.getZuoye().getUserinfo().getUserName();
		model.addAttribute("userinfoName", userinfoName);
		// 获取作业的章节
		ChapterEntity chapterEntity = zhangJieDao.findOne(gradeEntity.getZuoye().getChapterinfo());
		model.addAttribute("chapterEntity", chapterEntity);

		// 获取阶段
		PhaseEntity phaseEntity = phaseDao.findOne(chapterEntity.getPhaseId());
		
		/*
		 * ============================================柱状图==============================
		 * =====================================
		 */
		List<Integer> score = new ArrayList<Integer>();
		List<String> studentNames = new ArrayList<String>();
		// 获取这次作业中这个班里面的所有学生的成绩
		List<StudentEntity> studentEntities = stuDao.findByClassId(gradeEntity.getStudentinfoId().getClassId());
		for (StudentEntity studentEntity : studentEntities) {
			//GradeEntity c = grandeDao.findByStudentinfoId(studentEntity);
			GradeEntity c=grandeDao.cxGrande(Long.valueOf(zuoyeId), studentEntity.getStuId());
			if (c!=null) {
				System.out.println("EEEE:"+c);
				score.add(new Double(c.getSumScore()).intValue());
			}else {
				score.add(0);
			}
			studentNames.add(studentEntity.getStudentName());
		}
		ClassEntity className1=classDao.findOne(gradeEntity.getStudentinfoId().getClassId());
		model.addAttribute("className1",JSONArray.toJSONString(className1.getClassName()));
		model.addAttribute("studentScore",JSONArray.toJSONString(score));
		model.addAttribute("studentNames",JSONArray.toJSONString(studentNames));
		/*
		 * =============================================================================
		 * ==================================
		 */
		model.addAttribute("phaseEntity", phaseEntity);
		model.addAttribute("gradeEntity", gradeEntity);
		model.addAttribute("title1", list1);
		model.addAttribute("title2", list2);
		model.addAttribute("title3", list3);
		model.addAttribute("studentId", studentId);
		model.addAttribute("zuoyeId", zuoyeId);
		return "qt-assignment_details";
	}

	@RequestMapping("/qt-journal-teacher")
	public void journalTeacher() {

	}

	@RequestMapping("/qt-journal-history")
	public void journalhistory() {

	}

	@RequestMapping("/qt-journal-newest")
	public void journalNewest() {

	}

	@RequestMapping("/qt-journal-publish")
	public void journalPublish() {

	}
	@RequestMapping("/zuoye-success")
	public void zuoyeSuccess() {

	}
}